/**
 *
 * @author Bharath
 */
package Registration;

import Login.MySqlConnector;
import java.io.*;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class RegistrationDao {

    PreparedStatement ps ;
    Connection con;
    ResultSet rs;
    MySqlConnector ms = new MySqlConnector();
    boolean b;
    private String sdob;
    private String c1dob;
    private String c2dob;
    private String c3dob;
    private String marr_anv;
    private PreparedStatement pstmt;
    private ResultSet rs1;

    public boolean insert(RegistrationBean rb) {
        try {
            // Gets the conection from mysqlconnector file
            
            // get the values entered
            
            String memid = rb.getMemberid();
            System.out.println("member in registration dao"+memid);
            
            String pass = rb.getPassword();
            System.out.println("password in registration dao"+pass);
            
            String name = rb.getName();
            String surname = rb.getSurname();        
            String sex = rb.getSex();
            String caste = rb.getCaste();
            String dob1 = rb.getDob();
            String age = rb.getAge();
            System.out.println("age in registration dao is "+age);
            String[] bdob=dob1.split("-");
            String dob  =  bdob[2]+"-"+bdob[1]+"-"+bdob[0];

            String qualification = rb.getQualification();
            String prof = rb.getProf();
            String org = rb.getOrg();
            String res_add = rb.getRes_add();
            String off_add = rb.getOff_add();
            String state = rb.getState();
            String dist = rb.getDist();
            String mandal = rb.getMandal();
            String res_phone = rb.getRes_phone();
            String off_phone = rb.getOff_phone();
            String mobile = rb.getMobile();
            String email = rb.getEmail();
            String webadd = rb.getWebadd();
            String spouse_name = rb.getSpouse_name();           
            String ssex = rb.getSsex();

            String sdob1 = rb.getSdob();
            
            String[] bdob1=sdob1.split("-");
            System.out.println("Sdov1="+sdob1);
            System.out.println("bdob1.length="+bdob1.length);
            System.out.println("@@"+bdob1[0]);
            if (bdob1.length>1)
                sdob =  bdob1[2]+"-"+bdob1[1]+"-"+bdob1[0];

            //String mar_aniv = rb.getMar_aniv();
            String mar_aniv = rb.getMar_aniv();
            String[] marday2 = mar_aniv.split("-");
            System.out.println("Sdov1="+mar_aniv);
            System.out.println("marday2.length="+marday2.length);
            marr_anv="";
            if (marday2.length>1)
                marr_anv =  marday2[2]+"-"+marday2[1]+"-"+marday2[0];

            String squal = rb.getSqual();
            String sprof = rb.getSprof();
            String sorg = rb.getSorg();
            String c1name = rb.getC1name();
            String c1sex = rb.getC1sex();

            String c1dob1 = rb.getC1dob();
            String[] bdob2=c1dob1.split("-");
            c1dob="";
            if (bdob2.length>1)
                c1dob  =  bdob2[2]+"-"+bdob2[1]+"-"+bdob2[0];

            String c1qual = rb.getC1qual();
            String c2name = rb.getC2name();
            System.out.println("----- c2 name is ------ "+c2name);
            String c2sex = rb.getC2sex();

            String c2dob1 = rb.getC2dob();
            String[] bdob3=c2dob1.split("-");
            c2dob="";
            if (bdob3.length>1)
                c2dob  =  bdob3[2]+"-"+bdob3[1]+"-"+bdob3[0];

            String c2qual = rb.getC2qual();
            String c3name = rb.getC3name();
            System.out.println("----- c3 name is ------ "+c3name);
            String c3sex = rb.getC3sex();

            String c3dob1 = rb.getC3dob();
            String[] bdob4=c3dob1.split("-");
            c3dob="";
            if (bdob4.length>1)
                c3dob  =  bdob4[2]+"-"+bdob4[1]+"-"+bdob4[0];

            String c3qual = rb.getC3qual();
            String reffmemberid = rb.getReffmemberid();
            String status = rb.getStatus();
            InputStream file = rb.getFile();
            
            System.out.print(" Entered Member name :" + name);
            System.out.print(" Entered Spouse name :" + spouse_name);
            System.out.print(" Childern names are :" + c1name);
            System.out.println("age is "+age);
            System.out.println("res_phone  "+res_phone);
            // Prepared Statment
            /*String q = "insert into registration values('"
                    +"scf".concat(maxValue())+"','"
                    +"scf".concat(maxValue())+"','"
                    +name+"','"+surname+"','"
                    +age+"','"+sex+"','"+caste+"','"
                    +dob+"','"+qualification+"','"                                                                                                
                    +prof+"','"+org+"','"+res_add+"','"+ off_add+"','"
                    +state+"','"+dist+"','"+mandal+"','"+ res_phone+"','"
                    +off_phone+"','"+mobile+"','"+email+"','"+webadd+"','"
                    +spouse_name+"','"+ssex+"','"+sdob+"','"+marr_anv+"','"
                    +squal+"','"+sprof+"','"+sorg+"','"+c1name+"','"+c1sex+"','"
                    +c1dob+"','"+c1qual+"','"+c2name+"','"+c2sex+"','"+c2dob+"','"
                    +c2qual+"','"+c3name+"','"+c3sex+"','"+c3dob+"','"+c3qual+"','"
                    +reffmemberid+"','0','"+file+"',"+maxValue()+")";*/
            con = ms.Connect();
        String q="insert into registration values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            System.out.println("query="+q);
            ps = con.prepareStatement(q);
System.out.println("------------@#");
            ps.setString(1, "scf".concat(maxValue()));
            ps.setString(2, name);
            ps.setString(3, "scf".concat(maxValue()));
            
            ps.setString(4, surname);
            ps.setString(5, age);
            ps.setString(6, sex);
            ps.setString(7, caste);
             System.out.println("caste. .....");
            ps.setString(8, dob);
            System.out.println("date of birth........... :");
            ps.setString(9, qualification);
            ps.setString(10, prof);
            ps.setString(11, org);
            ps.setString(12, res_add);
            ps.setString(13, off_add);
            ps.setString(14, state);
            ps.setString(15, dist);
            ps.setString(16, mandal);
            ps.setString(17, res_phone);
            ps.setString(18, off_phone);
            ps.setString(19, mobile);
            ps.setString(20, email);
            ps.setString(21, webadd);
            ps.setString(22, spouse_name);
            ps.setString(23, ssex);
            ps.setString(24, sdob);
            ps.setString(25, mar_aniv);
            ps.setString(26, squal);
            ps.setString(27, sprof);
            ps.setString(28, sorg);
            ps.setString(29, c1name);
            ps.setString(30, c1sex);
            ps.setString(31, c1dob);
            ps.setString(32, c1qual);
            ps.setString(33, c2name);
            ps.setString(34, c2sex);
            ps.setString(35, c2dob);
            ps.setString(36, c2qual);
            ps.setString(37, c3name);
            ps.setString(38, c3sex);
            ps.setString(39, c3dob);
            ps.setString(40, c3qual);
            ps.setString(41, reffmemberid);
            ps.setString(42, "1");
            ps.setBinaryStream(43, file, 10000);
            ps.setString(44, maxValue());

            int i = ps.executeUpdate();
            System.out.print("Value of I is ------------ > > > >"+i);
            if (i == 1)
            {               
                System.out.print("inside if i == 1 ");
                return true ;
            }
            else {
                    System.out.print("inside else i not equal to 1 ");
                return false;
            }
        } catch (Exception ex) {
            Logger.getLogger(RegistrationDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
        }

    /* --------------------------------- MaxValue() -------------------------------------*/
    public String maxValue() throws Exception {
        int p = 0;
        String k = "";
        try {
            con = ms.Connect();
            String query =  "select max(sno) from registration ;" ;
            pstmt = con.prepareStatement(query);
            rs1 = pstmt.executeQuery();
            if (rs1.next()== true)
            {
             p = rs1.getInt(1) + 1;
            }//end of if
            k = new Integer(p).toString();           
        }//end of try
        catch (Exception e) {
            e.printStackTrace();
        }//end of catch
        return k;
    }
/*-------------------------------------- Update Method---------------------------------------*/

    public boolean update_member(RegistrationBean rb)
    {
        try {
            // Gets the conection from mysqlconnector file
            con = ms.Connect();
            
            String rcheck = rb.getMemberid();
            System.out.println("member in registration dao "+rcheck);

            //String pass = rb.getPassword();
            //System.out.println("password in registration dao"+pass);

            String name = rb.getName();
            String surname = rb.getSurname();
            String sex = rb.getSex();
            String caste = rb.getCaste();
            String dob1 = rb.getDob();
            String age = rb.getAge();
            System.out.println("age in registration dao is "+age);
            String[] bdob=dob1.split("-");
            String dob  =  bdob[2]+"-"+bdob[1]+"-"+bdob[0];

            String qualification = rb.getQualification();
            String prof = rb.getProf();
            String org = rb.getOrg();
            String res_add = rb.getRes_add();
            String off_add = rb.getOff_add();
            String state = rb.getState();
            String dist = rb.getDist();
            String mandal = rb.getMandal();
            String res_phone = rb.getRes_phone();
            String off_phone = rb.getOff_phone();
            String mobile = rb.getMobile();
            String email = rb.getEmail();
            String webadd = rb.getWebadd();
            String spouse_name = rb.getSpouse_name();
            String ssex = rb.getSsex();

            String sdob1 = rb.getSdob();

            String[] bdob1=sdob1.split("-");
            System.out.println("Sdob1="+sdob1);
            System.out.println("bdob1.length="+bdob1.length);
            System.out.println("@@"+bdob1[0]);
            if (bdob1.length>1)
                sdob =  bdob1[2]+"-"+bdob1[1]+"-"+bdob1[0];

            //String mar_aniv = rb.getMar_aniv();
            String mar_aniv = rb.getMar_aniv();
            String[] marday2 = mar_aniv.split("-");
            System.out.println("Sdov1="+mar_aniv);
            System.out.println("marday2.length="+marday2.length);
            marr_anv="";
            if (marday2.length>1)
                marr_anv =  marday2[2]+"-"+marday2[1]+"-"+marday2[0];

            String squal = rb.getSqual();
            String sprof = rb.getSprof();
            String sorg = rb.getSorg();
            String c1name = rb.getC1name();
            String c1sex = rb.getC1sex();

            String c1dob1 = rb.getC1dob();
            String[] bdob2=c1dob1.split("-");
            c1dob="";
            if (bdob2.length>1)
                c1dob  =  bdob2[2]+"-"+bdob2[1]+"-"+bdob2[0];

            String c1qual = rb.getC1qual();
            String c2name = rb.getC2name();
            String c2sex = rb.getC2sex();

            String c2dob1 = rb.getC2dob();
            String[] bdob3=c2dob1.split("-");
            c2dob="";
            if (bdob3.length>1)
                c2dob  =  bdob3[2]+"-"+bdob3[1]+"-"+bdob3[0];

            String c2qual = rb.getC2qual();
            String c3name = rb.getC3name();
            String c3sex = rb.getC3sex();

            String c3dob1 = rb.getC3dob();
            String[] bdob4=c3dob1.split("-");
            c3dob="";
            if (bdob4.length>1)
                c3dob  =  bdob4[2]+"-"+bdob4[1]+"-"+bdob4[0];

            String c3qual = rb.getC3qual();
            String reffmemberid = rb.getReffmemberid();
            InputStream file = rb.getFile();

            // Prepared Statment
            String q = "update registration set " +
                    "name = ?, surname = ?,age = ?, sex = ?," +
                    " caste = ?,  dob = ?, qual = ?, prof=?,org = ?" +
                    ",res_add = ?, off_add = ?, state = ?, dist = ?," +
                    " mandal = ?,res_phone = ?, off_phone = ?, mobile = ?," +
                    "email = ?,webadd=?,spouse_name = ?,ssex =?,sdob = ?," +
                    "mar_aniv=?,squal=?, sprof=?, sorg=?,c1name = ?,c1sex =?," +
                    "c1dob=?,c1qual=? ,c2name=?,c2sex = ?,c2dob=?,c3qual=?" +
                    ",c3name = ?,c3sex = ?,c3dob=?,c3qual=?,reffmemberid=?,profpic = ?" +
                    " where memberid = ? ;";
 
            System.out.println("query="+q);
            ps = con.prepareStatement(q);
            System.out.println("prepare Statement is "+ps);
 
           
            ps.setString(1, name);
            ps.setString(2, surname);
            ps.setString(3, age);
            ps.setString(4, sex);
            ps.setString(5, caste);
            ps.setString(6, dob);
            ps.setString(7, qualification);
            ps.setString(8, prof);
            ps.setString(9, org);
            ps.setString(10, res_add);
            ps.setString(11, off_add);
            ps.setString(12, state);
            ps.setString(13, dist);
            ps.setString(14, mandal);
            ps.setString(15, res_phone);
            ps.setString(16, off_phone);
            ps.setString(17, mobile);
            ps.setString(18, email);
            ps.setString(19, webadd);
            ps.setString(20, spouse_name);
            ps.setString(21, ssex);
            ps.setString(22, sdob);
            ps.setString(23, mar_aniv);
            ps.setString(24, squal);
            ps.setString(25, sprof);
            ps.setString(26, sorg);
            ps.setString(27, c1name);
            ps.setString(28, c1sex);
            ps.setString(29, c1dob);
            ps.setString(30, c1qual);
            ps.setString(31, c2name);
            ps.setString(32, c2sex);
            ps.setString(33, c2dob);
            ps.setString(34, c2qual);
            ps.setString(35, c3name);
            ps.setString(36, c3sex);
            ps.setString(37, c3dob);
            ps.setString(38, c3qual);
            ps.setString(39, reffmemberid);
            ps.setBinaryStream(40, file, 10000);
            ps.setString(41, rcheck);

            int i = ps.executeUpdate();
            System.out.print("Value of I is ------------ > > > >"+i);
            if (i == 1)
            {
                System.out.print("inside if i == 1 ");
                return true ;
            }
            else {
                    System.out.print("inside else i not equal to 1");
                return false;
            }
        } catch (Exception ex) {
            Logger.getLogger(RegistrationDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
       }
}

